﻿using Microsoft.AspNetCore.Mvc;
using UpperComputer.Application;
using YFurion.Application;
using YFurion.Controllers;

namespace UpperComputer.Web.Entry.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [ApiDescriptionSettings(ApiGroupConst.USER_AUTHORIZATION)]
    public class UserController : AuthorizeController
    {
        private readonly IBaseUserService _userService;
        private readonly IBaseRoleService _roleService;

        /// <summary>
        /// 用户控制器构造函数
        /// </summary>
        /// <param name="userService">用户服务</param>
        /// <param name="roleService">角色服务</param>
        public UserController(
            IBaseUserService userService,
            IBaseRoleService roleService
            )
        {
            _userService = userService;
            _roleService = roleService;
        }

        /// <summary>
        /// 获取用户集合
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<BaseUserResultDto>> Get([FromQuery] BaseUserResultRequestDto input)
        {
            PagedResultDto<BaseUserResultDto> pagedResultDto = await _userService.GetAll(input);
            return pagedResultDto;
        }

        /// <summary>
        /// 根据id获取用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BaseUserResultDto> Get(long id)
        {
            return await _userService.Get(id);
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public async Task<IReadOnlyList<BaseRoleResultDto>> GetRoles()
        {
            PagedResultDto<BaseRoleResultDto> pagedResultDto = await _roleService.GetAll(new BaseRoleResultRequestDto());
            IReadOnlyList<BaseRoleResultDto> baseRoleResultDtos = pagedResultDto.Items;
            return baseRoleResultDtos;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [YUnitOfWork]
        public async Task<BaseUserResultDto> Post([FromForm] BaseUserCreateDto input)
        {
            BaseUserResultDto baseUserResultDto = await _userService.Post(input);
            return baseUserResultDto;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [YUnitOfWork]
        public async Task<BaseUserResultDto> Put([FromForm] BaseUserDto input)
        {
            BaseUserResultDto userInfoResultDto = await _userService.Put(input);
            return userInfoResultDto;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseUserResultDto> PutRecover(EntityDto input)
        {
            BaseUserResultDto baseUserResultDto = await _userService.Recover(input);
            return baseUserResultDto;
        }

        /// <summary>
        /// 启用用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseUserResultDto> PutEnable(EntityDto input)
        {
            BaseUserResultDto baseUserResultDto = await _userService.Enable(input);
            return baseUserResultDto;
        }

        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseUserResultDto> PutDisable(EntityDto input)
        {
            BaseUserResultDto baseUserResultDto = await _userService.Disable(input);
            return baseUserResultDto;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EntityDto> Delete(long id)
        {
            await _userService.Delete(id );
            return new EntityDto { Id = id };
        }

        /// <summary>
        /// 删除头像
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseUserDto> PutDeletePhoto(BaseUserDto input)
        {
            await _userService.DeletePhoto(input);
            return input;
        }
    }
}
